package com.example.dynamicschedule.service;




import com.example.dynamicschedule.config.ScanningCleaningConfig;
import com.example.dynamicschedule.utils.FileDownloadUtil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 通过esb获取人工客服放到oss服务商的json文件
 */
//@Component("CustomerService")

@Service
public class CustomerService {
    private Logger logger = LoggerFactory.getLogger(CustomerService.class);
    @Autowired
    private ScanningCleaningConfig scanningCleaningConfig;
    @Autowired
    private FileDownloadUtil fileDownloadUtil;
    @Autowired
    PeopleCallService peopleCallService;


    /**
     * 读取ESB文件
     *
     * @param feedBackConfigMap
     * @return
     */
   /* public String getCustomerJsonFile(Map<String, CkFeedbackConfigTable> feedBackConfigMap) throws Exception {
        E_40013000101_Req model = handleModel();
        logger.info("------获取人工客服esb里oss服务信息------start:param-" + JSONObject.toJSONString(model));
        //调用esb rpc接口
        E_40013000101_Res e_40013000101_res = i40013000101.request(model);
       *//* E_40013000101_Res e_40013000101_res = new E_40013000101_Res();
        HeadOut head=new HeadOut();
        head.setDownFilePath("upload/483779a416d14b5091f104362871644b.json");
        e_40013000101_res.setHead(head);*//*
        logger.info("-----获取人工客服esb里oss服务信息 " + JSONObject.toJSONString(e_40013000101_res));
        if (null == e_40013000101_res || null == e_40013000101_res.getHead() || null == e_40013000101_res.getHead().getDownFilePath() || "".equals(e_40013000101_res.getHead().getDownFilePath())) {
            logger.error("-------获取人工客服esb里oss服务信息头部文件地址是空------end-----result:" + JSONObject.toJSONString(e_40013000101_res));
            return JSONObject.toJSONString(e_40013000101_res);
        }
        try {
            //从esb信息文件路径中获取文件名称
            String filePath = e_40013000101_res.getHead().getDownFilePath().trim();
            String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
            logger.info("------人工客服esb里oss服务信息fileName:" + fileName);


            //下载文件到服务器
            fileDownloadUtil.downLoadLocal(filePath, scanningCleaningConfig.getPeopleDownloadJsonPath(), fileName);
            //读取下载的文件
            String customerJson = handleFile(scanningCleaningConfig.getPeopleDownloadJsonPath() + fileName);
            //保存人工json数据
            peopleCallService.peopleCallService(customerJson, feedBackConfigMap);

        } catch (IOException e) {
            logger.error("--------人工客服下载oss服务上的json文件异常---------");
            throw e;
        } catch (ParseException e) {
            logger.error("---------人工客服数据处理异常：" + e.getMessage());
            throw e;
        } catch (Exception e) {
            logger.error("-----人工客服读取json文件异常:" + e.getMessage());
            throw e;
        }
        logger.info("-------获取人工客服esb里oss服务信息------end--SUCCESS");
        return JSONObject.toJSONString(e_40013000101_res);
    }*/

    public String handleFile(String filePath) throws Exception {
        String customerJson = fileDownloadUtil.readESBJsonFile(filePath);
        return customerJson;
    }

    /*public E_40013000101_Req handleModel() {
        E_40013000101_Req model = new E_40013000101_Req();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMdd");
        Head head = new Head();
        head.setServiceId("40013000101");
        head.setRequestServerId("168.5.100.179");
        head.setSystemId("0106");
        head.setBankSrlNo(simpleDateFormat.format(new Date()));
        head.setTranDate(simpleDate.format(new Date()));
        head.setTranTimestamp(simpleDateFormat.format(new Date()));
        model.setHead(head);
        model.setSyncDt(simpleDate.format(new Date()));
        model.setTxnNo("L6500");
        return model;
    }*/

    /***
     *  返回oss文件路径
     * @param CardType 1网贷  2 信贷
     * @return
     */
   /* public String getEsbInterface(String CardType) {
        E_40013000101_Req model = handleModel(CardType);
        logger.info("------获取人工客服esb里oss服务信息------start:param-" + JSONObject.toJSONString(model));
        //调用esb rpc接口
        E_40013000101_Res e_40013000101_res = i40013000101.request(model);

        logger.info("-----获取人工客服esb里oss服务信息 " + JSONObject.toJSONString(e_40013000101_res));
        if (null == e_40013000101_res || null == e_40013000101_res.getHead() || null == e_40013000101_res.getHead().getDownFilePath() || "".equals(e_40013000101_res.getHead().getDownFilePath())) {
            logger.error("-------获取人工客服esb里oss服务信息头部文件地址是空------end-----result:" + JSONObject.toJSONString(e_40013000101_res));
            return JSONObject.toJSONString(e_40013000101_res);
        }
        String filePath = null;
        try {
            //从esb信息文件路径中获取文件名称
            filePath = e_40013000101_res.getHead().getDownFilePath().trim();
            return filePath;
        } catch (Exception e) {
            logger.error("-----获取人工客服esb里oss头部信息文件路径异常");
        }
        return filePath;
    }*/

   /* public E_40013000101_Req handleModel(String cardType) {
        E_40013000101_Req model = new E_40013000101_Req();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMdd");
        Head head = new Head();
        head.setServiceId("40013000101");
        head.setRequestServerId("168.5.100.179");
        head.setSystemId("0106");
        head.setTranDate(simpleDate.format(new Date()));
        head.setBankSrlNo(simpleDateFormat.format(new Date()));
        head.setTranTimestamp(simpleDateFormat.format(new Date()));
        model.setHead(head);
        model.setSyncDt(simpleDate.format(new Date()));
        //网贷
        if("1".equals(cardType)) {
            model.setTxnNo("L6500");
        }
        //信贷
        if("2".equals(cardType)){
            model.setTxnNo("L6501");
        }
        return model;
    }*/
}
